/*
 * 
 */
package fr.univtln.superplanning.gui.planning;

import java.awt.Canvas;

import org.eclipse.swt.SWT;
import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

import fr.univtln.superplanning.controller.PlanningViewerController;

/**
 * Helped with
 * http://www.blogs.uni-osnabrueck.de/rotapken/2011/01/05/java-howto-
 * embed-swt-widget-into-swing-jframe/
 */
public class PlanningViewer extends Canvas {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Thread browserThread;
	private Browser browser;
	private String planningUrl;
	private PlanningViewerController planningViewerController;

	// Obligatory for linux systems
	static {
		System.setProperty("sun.awt.xembedserver", "true");

	}

	public PlanningViewer() {

	}

	public PlanningViewer(PlanningViewerController planningViewerController) {
		this();
		this.planningViewerController = planningViewerController;
	}

	// private PlanningViewerController planningViewerController;

	/**
	 * Connect this canvas to a SWT shell with a Browser component and starts a
	 * background thread to handle SWT events. This method waits until the
	 * browser component is ready.
	 */
	public void connect() {
		if (this.browserThread == null) {
			final Canvas canvas = this;
			this.browserThread = new Thread() {
				@Override
				public void run() {
					try {
						Display display = new Display();
						Shell shell = SWT_AWT.new_Shell(display, canvas);
						shell.setLayout(new FillLayout());

						synchronized (this) {
							browser = new Browser(shell, SWT.NONE);
							this.notifyAll();
						}

						shell.open();
						while (!isInterrupted() && !shell.isDisposed()) {
							if (!display.readAndDispatch()) {
								display.sleep();
							}
						}
						shell.dispose();
						display.dispose();
					} catch (Exception e) {
						interrupt();
					}
				}
			};
			this.browserThread.start();
		}

		// Wait for the Browser instance to become ready
		synchronized (this.browserThread) {
			while (this.browser == null) {
				try {
					this.browserThread.wait(100);
				} catch (InterruptedException e) {
					this.browser = null;
					this.browserThread = null;
					break;
				}
			}
		}
	}

	/**
	 * Returns the Browser instance. Will return "null" before "connect()" or
	 * after "disconnect()" has been called.
	 */
	public Browser getBrowser() {
		return this.browser;
	}

	/**
	 * Stops the swt background thread.
	 */
	public void disconnect() {
		if (browserThread != null) {
			browser = null;
			browserThread.interrupt();
			browserThread = null;
		}
	}

	/**
	 * Ensures that the SWT background thread is stopped if this canvas is
	 * removed from it's parent component (e.g. because the frame has been
	 * disposed).
	 */
	@Override
	public void removeNotify() {
		super.removeNotify();
		disconnect();
	}

	public void updateBrowser() {
		connect();

		// Now we can open a webpage, but remember that we have
		// to use the SWT thread for this.
		browser.getDisplay().asyncExec(new Runnable() {
			@Override
			public void run() {
				getBrowser().setUrl(planningUrl);
			}
		});
		

	}

	public String getPlanningUrl() {
		return planningUrl;
	}

	public void setPlanningUrl(String planningUrl) {
		this.planningUrl = planningUrl;
	}

}
